//
//  CRImportNewsViewController.m
//  NewAge
//
//  Created by dllo on 15/12/3.
//  Copyright © 2015年 ColorRevolution. All rights reserved.
//

#import "CRImportNewsViewController.h"
#import "CRNewsModel.h"
#import "CRNewsTableViewCell.h"
#import "CRPicTableViewCell.h"
#import "CRSpecialNewsTableViewCell.h"
#import "CRNewsDetailViewController.h"
#import "CRPicNewsViewController.h"
#import "CRSpecialNewsViewController.h"

@interface CRImportNewsViewController ()<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, strong) UIView *headBgView;
@property (nonatomic, strong) UILabel *titleLabel;
@property (nonatomic, strong) UILabel *contentLabel;
@property (nonatomic, strong) UIButton *backButton;
@property (nonatomic, strong) UITableView *tableView;

@property (nonatomic, strong) NSMutableArray *importArr;

@end

@implementation CRImportNewsViewController

#pragma mark - init

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.importArr = [NSMutableArray array];
    }
    return self;
}



#pragma mark - createData

- (void)createData {
    [self checkNetWorkWithblock:^(NetStatus status) {
        if (status == WiFi || status == WWAN) {
            [AfNetWorkingManager getInfoWithUrl:@"http://c.m.163.com/nc/article/list/T1348648141035/0-20.html" block:^(NSDictionary *resultDic, NSError *error) {
                if (resultDic) {
                    self.importArr = [CRNewsModel baseModelByArr:resultDic[@"T1348648141035"]];
                    
                    [self.tableView reloadData];
                }
            }];
        }
    }];
}



#pragma mark - createViews

- (void)createViews {
    self.headBgView = [[UIView alloc] init];
    _headBgView.backgroundColor = [UIColor colorWithRed:0.925f green:0.239f blue:0.251f alpha:1.00f];
    [self.view addSubview:_headBgView];
    
    self.titleLabel = [[UILabel alloc] init];
    _titleLabel.textColor = [UIColor whiteColor];
    _titleLabel.text = @"24小时要闻";
    _titleLabel.textAlignment = NSTextAlignmentCenter;
    _titleLabel.font = [UIFont fontWithName:@"Helvetica-Bold" size:26];
    [self.view addSubview:_titleLabel];
    
    self.contentLabel = [[UILabel alloc] init];
    _contentLabel.textColor = [UIColor whiteColor];
    _contentLabel.font = [UIFont systemFontOfSize:16];
    _contentLabel.textAlignment = NSTextAlignmentCenter;
    _contentLabel.text = @"--聚焦今日时事  浓缩新闻精华--";
    [self.headBgView addSubview:_contentLabel];
    
    self.backButton = [UIButton buttonWithType:UIButtonTypeSystem];
    [_backButton setBackgroundImage:[UIImage imageNamed:@"iconfont-fanhui.png"] forState:UIControlStateNormal];
    [_backButton addTarget:self action:@selector(backAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:_backButton];
    
    self.tableView = [[UITableView alloc] initWithFrame:self.view.frame style:UITableViewStylePlain];
    _tableView.separatorStyle = NO;
    [self.view addSubview:_tableView];
    
    [_tableView registerClass:[CRNewsTableViewCell class] forCellReuseIdentifier:@"nReuse"];
    [_tableView registerClass:[CRSpecialNewsTableViewCell class] forCellReuseIdentifier:@"snReuse"];
    [_tableView registerClass:[CRPicTableViewCell class] forCellReuseIdentifier:@"pReuse"];
    
    _tableView.dataSource = self;
    _tableView.delegate = self;
    
    [self setLayout];
}



#pragma mark - setLayout

- (void)setLayout {
    [_headBgView mas_makeConstraints:^(MASConstraintMaker *make) {
        Top(self.view, 0);
        Left(self.view, 0);
        Right(self.view, 0);
        Bottom(_tableView.mas_top, 0);
    }];
    
    [_titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        CenterX(_headBgView);
        make.centerY.equalTo(_headBgView).with.offset(-10);
        WequalNum(150);
        HequalNum(35);
    }];
    
    [_contentLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        CenterX(_headBgView);
        Top(_titleLabel.mas_bottom, 20 * HEIGHT / 667.0);
        HequalNum(20);
        WequalNum(240);
    }];
    
    [_backButton mas_makeConstraints:^(MASConstraintMaker *make) {
        Top(_headBgView.mas_top, 27);
        Left(_headBgView.mas_left, 15);
        HequalNum(20);
        WequalNum(20);
    }];
    
    [_tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        Top(_headBgView.mas_bottom, 0);
        Left(self.view, 0);
        Right(self.view, 0);
        Bottom(self.view, 0);
        HequalNum(HEIGHT - 200 * HEIGHT / 667.0);
    }];
}



#pragma mark - ViewMethod

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    
    [self createViews];
    
    [self createData];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



#pragma mark - TableViewDataSource & Delegate

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.importArr.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if ([self.importArr[indexPath.row] imgextra]) {
        return 124 * HEIGHT / 667.0;
    }
    else {
        return 94 * HEIGHT / 667.0;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    CRNewsModel *news = self.importArr[indexPath.row];
    
    if ([news.skipType isEqualToString:@"special"]) {
        CRSpecialNewsTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"snReuse" forIndexPath:indexPath];
        cell.news = news;
        return cell;
    }
    else {
        if (news.imgextra) {
            CRPicTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"pReuse" forIndexPath:indexPath];
            cell.news = news;
            return cell;
        }
        else {
            CRNewsTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"nReuse" forIndexPath:indexPath];
            cell.news = news;
            return cell;
        }
    }
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    CRNewsModel *news = self.importArr[indexPath.row];
    
    if ([news.skipType isEqualToString:@"special"]) {
        
        NSString *strURL = [NSString stringWithFormat:@"http://c.3g.163.com/nc/special/%@.html", news.specialID];
        
        CRSpecialNewsViewController *snVC = [[CRSpecialNewsViewController alloc] init];
        snVC.url = strURL;
        snVC.myTitle = news.title;
        [self.navigationController pushViewController:snVC animated:YES];
    }
    else {
        if (news.imgextra) {
            
            NSString *str = news.photosetID;
            NSString *firstStr = [str substringFromIndex:9];
            NSString *secondStr = [str substringWithRange:NSMakeRange(4, 4)];
            NSString *strURL = [NSString stringWithFormat:@"http://c.3g.163.com/photo/api/set/%@/%@.json", secondStr, firstStr];
            
            CRPicNewsViewController *pnVC = [[CRPicNewsViewController alloc] init];
            pnVC.url = strURL;
            pnVC.myTitle = news.title;
            [self.navigationController pushViewController:pnVC animated:YES];
        }
        else {
            CRNewsDetailViewController *ndVC = [[CRNewsDetailViewController alloc] init];
            ndVC.str = news.url_3w;
            ndVC.myTitle = news.title;
            [self.navigationController pushViewController:ndVC animated:YES];
        }
    }
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    if (scrollView == self.tableView) {
        if (scrollView.contentOffset.y > 0 && scrollView.contentOffset.y < 136 * HEIGHT / 667.0) {
            [_tableView mas_updateConstraints:^(MASConstraintMaker *make) {
                HequalNum(HEIGHT - 200 * HEIGHT / 667.0 + scrollView.contentOffset.y);
            }];
            
            [_titleLabel mas_updateConstraints:^(MASConstraintMaker *make) {
               make.centerY.equalTo(_headBgView).with.offset(10 * (scrollView.contentOffset.y - 68 * HEIGHT / 667.0) / 68 * HEIGHT / 667.0);
            }];
            
            self.titleLabel.font = [UIFont fontWithName:@"Helvetica-Bold" size:26 - scrollView.contentOffset.y / 16 * HEIGHT / 667.0];
            self.contentLabel.hidden = YES;
        }
        else if (scrollView.contentOffset.y >= 136 * HEIGHT / 667.0) {
            [_tableView mas_updateConstraints:^(MASConstraintMaker *make) {
                HequalNum(HEIGHT - 64 * HEIGHT / 667.0);
            }];
            
            self.titleLabel.font = [UIFont fontWithName:@"Helvetica-Bold" size:26 - 136.0 / 16 * HEIGHT / 667.0];
            
            self.contentLabel.hidden = YES;
        }
        else {
            self.contentLabel.hidden = NO;
        }
    }
}



#pragma mark - ButtonAction 

- (void)backAction:(UIButton *)button {
    [self.navigationController popViewControllerAnimated:YES];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
